package com.hylink.util;

import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.util.List;

/**
 * HTTP工具类
 *
 * @author JXL
 */
public class HttpUtil {
    public static String post(String url, List<NameValuePair> param,
                              String charset) {
        String out = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpPost httpPost = new HttpPost(new URI(url));
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(3000000).setConnectTimeout(3000000)
                    .build();// 设置请求和传输超时时间
            httpPost.setConfig(requestConfig);
            if (null != param)
                httpPost.setEntity(new UrlEncodedFormEntity(param, charset));
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            try {
                if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    out = EntityUtils.toString(httpResponse.getEntity(),
                            charset);
                }
            } finally {
                httpResponse.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return out;
    }

    public static String post(String url, String param, String charset) {
        String out = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpPost httpPost = new HttpPost(new URI(url));
            if (null != param) {
                StringEntity reqEntity = new StringEntity(param, charset);
                // 设置类型
                reqEntity.setContentType("application/x-www-form-urlencoded");
                // 设置请求的数据
                httpPost.setEntity(reqEntity);
            }
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(3000000).setConnectTimeout(3000000)
                    .build();// 设置请求和传输超时时间
            httpPost.setConfig(requestConfig);
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            try {
                if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    out = EntityUtils.toString(httpResponse.getEntity(),
                            charset);
                }
            } finally {
                httpResponse.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return out;
    }

    public static String get(String url, String charset) {
        String out = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpGet httpPost = new HttpGet(new URI(url));
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(100000).setConnectTimeout(100000).build();// 设置请求和传输超时时间
            httpPost.setConfig(requestConfig);
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            try {
                if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    out = EntityUtils.toString(httpResponse.getEntity(),
                            charset);
                }
            } finally {
                httpResponse.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return out;
    }

    public static String postxml(String url, String xml, String charset) {
        String out = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            HttpPost httpPost = new HttpPost(new URI(url));
            if (null != xml) {
                StringEntity entity = new StringEntity(xml);
                httpPost.addHeader("Content-Type", "text/xml");
                httpPost.setEntity(entity);
            }
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
            try {
                if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    out = EntityUtils.toString(httpResponse.getEntity(),
                            charset);
                }
            } finally {
                httpResponse.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return out;
    }
}
